package com.example.controlexercise;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<View> viewPagerPhoto = new ArrayList<>();
    private ViewPager viewPager;
    private ImageView indicator;
    private ImageView[] indicators;
    private LinearLayout group;
    public boolean isContinue = true;
    private int index = 0;
    private Handler viewPagerHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            viewPager.setCurrentItem(msg.what);
            super.handleMessage(msg);
        }
    };
    private ListView listView;
    ArrayList<getItem> list = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setList();
        initListView();
    }

    //设置菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    //设置菜单的选择事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.about :
                Toast.makeText(MainActivity.this,R.id.about+" ",Toast.LENGTH_SHORT).show();
                break;
            case R.id.see:
                Toast.makeText(MainActivity.this,R.id.see+" ",Toast.LENGTH_SHORT).show();
                break;
            case R.id.set:
                Intent intent = new Intent(MainActivity.this,setView.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initView(){
        setViewPagerPhoto();
        group = findViewById(R.id.group);
        viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(new viewPagerAdapter(viewPagerPhoto));

        //动态加载指示器
        indicators = new ImageView[viewPagerPhoto.size()];
        for (int i =0;i<indicators.length;i++){
            indicator = new ImageView(this);
            indicator.setLayoutParams(new LinearLayout.LayoutParams(45,45));
            indicator.setPadding(5,5,5,5);
            indicator.setId(i);
            indicators[i] = indicator;
            if (i==0){
                indicators[i].setBackgroundResource(R.drawable.black);
            }else{
                indicators[i].setBackgroundResource(R.drawable.gray);
            }
            group.addView(indicators[i]);
        }

        //与图片联动
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i =0;i<viewPagerPhoto.size();i++){
                    if (i==position){
                        indicators[i].setBackgroundResource(R.drawable.black);
                    }else{
                        indicators[i].setBackgroundResource(R.drawable.gray);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    clickIndicator();
                    if (isContinue){
                        viewPagerHandler.sendEmptyMessage(index);
                        try {
                            whatOption();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
    public void whatOption() throws InterruptedException {
        index++;
        if (index>viewPagerPhoto.size()-1){
            index=0;
        }
        Thread.sleep(2000);
    }

    //指示器点击事件

    public void clickIndicator(){
        for (int i =0;i<indicators.length;i++){

            indicators[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPagerHandler.sendEmptyMessage(v.getId());
                }
            });
        }
    }

    public void setViewPagerPhoto(){
        ImageView p1= new ImageView(this);
        p1.setBackgroundResource(R.drawable.p1);
        viewPagerPhoto.add(p1);
        ImageView p2= new ImageView(this);
        p2.setBackgroundResource(R.drawable.p2);
        viewPagerPhoto.add(p2);
        ImageView p3= new ImageView(this);
        p3.setBackgroundResource(R.drawable.p3);
        viewPagerPhoto.add(p3);
        ImageView p4= new ImageView(this);
        p4.setBackgroundResource(R.drawable.p4);
        viewPagerPhoto.add(p4);
    }

    public void initListView(){
        listView = findViewById(R.id.listView);
        listView.setAdapter(new listItemAdapter(this,R.layout.listlayout,list));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(MainActivity.this,switchTest.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(MainActivity.this,login.class);
                        startActivity(intent1);
                        break;
                    case 2:
                        Intent intent2 = new Intent(MainActivity.this,okHttpAndJson.class);
                        startActivity(intent2);
                }
            }
        });
    }

    public void setList(){
           getItem item1 = new getItem(R.drawable.cape,"注册账号");
           list.add(item1);
           getItem item2 = new getItem(R.drawable.cityscape,"登录");
           list.add(item2);
           getItem item3 = new getItem(R.drawable.fields,"使用okHttp进行接口测试并用GSON进行解析数据");
           list.add(item3);
           getItem item4 = new getItem(R.drawable.sea,"sea");
           list.add(item4);
           getItem item5 = new getItem(R.drawable.spruce,"spruce");
           list.add(item5);
    }

}